/// <reference path="BaseNode.ts" />

namespace bt {
    /**
     * 动作的基类
     *
     * @export
     * @class Action
     * @extends {BaseNode}
     */
    export class Action extends BaseNode {
        constructor(params: any) {
            super(params);
            this.category = CategoryType.ACTION
            this.name = 'Action'
        }
    }

    /**
     * 错误的动作
     *
     * @export
     * @class Error
     * @extends {Action}
     */
    export class Error extends Action {
        constructor(params?: any) {
            super(params);
            this.name = 'Error';
        }
        
        tick(tick: Tick) : ActionStatus {
            return ActionStatus.ERROR;
        }
    }

    /**
     * 失败的动作
     *
     * @export
     * @class Failer
     * @extends {Action}
     */
    export class Failer extends Action {
        constructor(params?: any) {
            super(params);
            this.name = 'Failer';
        }
    
        tick(tick: Tick) : ActionStatus {
            return ActionStatus.FAILURE;
        }
    }

    /**
     * 运行中的动作
     *
     * @export
     * @class Runner
     * @extends {Action}
     */
    export class Runner extends Action {
        constructor(params?: any) {
            super(params);
            this.name = 'Runner';
        }
        
        tick(tick: Tick) : ActionStatus {
            return ActionStatus.RUNNING;
        }
    }

    /**
     * 输出
     *
     * @export
     * @class Logger
     * @extends {Action}
     */
    export class Logger extends Action {
        constructor(params?: any) {
            super(params);
            this.name = 'Logger';
        }
        
        tick(tick: Tick) : ActionStatus {
            console.log(this.properties.msg)
            return ActionStatus.SUCCESS;
        }
    }

    /**
     * 成功的动作
     *
     * @export
     * @class Succeeder
     * @extends {Action}
     */
    export class Succeeder extends Action {
        constructor(params?: any){
            super(params);
            this.name = 'Succeeder';
        }
        
        tick(tick: Tick) : ActionStatus {
            return ActionStatus.SUCCESS;
        }
    }

    /**
     * 等待的动作
     *
     * @export
     * @class Wait
     * @extends {Action}
     */
    export class Wait extends Action {
        m_endTime: number = 0;  // 等待时间
        constructor(params?: any) {
            super(params);
            params = params || {milliseconds: 0};
            this.name = 'Wait';
            this.title = 'Wait <milliseconds>ms';
            this.properties = params;
            this.m_endTime = params.milliseconds || 0;
        }
    
        open(tick: Tick) {
            let startTime = (new Date()).getTime();
            tick.blackboard.set('startTime', startTime, tick.tree.id, this.id);
        }
        
        tick(tick: Tick) : ActionStatus {
            let currentTime = (new Date()).getTime();
            let startTime = tick.blackboard.get('startTime', tick.tree.id, this.id);
            if (currentTime - startTime > this.m_endTime) {
                return ActionStatus.SUCCESS;
            }
            return ActionStatus.RUNNING;
        }
    }
}